package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceFtService;
import com.eastjet.equipment.service.DeviceRkbService;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.DeviceFt;
import com.eastjet.maintenance.domain.DeviceRkb;
import com.eastjet.maintenance.domain.DeviceRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rkb")
public class RkbController {

    @Autowired
    private DeviceRkbService deviceRkbService;




    @RequestMapping("/list")
    public Page<DeviceRkb> getFTList(
            @RequestBody Map<String, Object> map)
    {

        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceRkbService.getDeviceRkb(0, 0, "");
        }

        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return  deviceRkbService.getDeviceRkb(page, size, condition);
    }


    @RequestMapping("/add")
    public String addFT(@RequestBody Map<String, Object> map) {
        System.out.println("addmap:" + map);
        String TableName;
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else {
            TableName = (String) map.get("TableName");
            DeviceRkb deviceRkb = new DeviceRkb();
            System.out.println("addmap:" + map);
            deviceRkb.setRkbName((String) map.get("name"));
            deviceRkb.setRkbState(Integer.valueOf(map.get("state").toString()));
            deviceRkb.setEquipmentId((Integer) map.get("equipmentId"));
            deviceRkb.setRkbTypeId((String) map.get("rkbType"));
            deviceRkb.setPartId((String) map.get("rkbPartId"));
            deviceRkb.setRmId((String) map.get("rkbRm"));
            deviceRkb.setDescription((String) map.get("description"));
            deviceRkb.setRkbNotes((String) map.get("notes"));
            return deviceRkbService.addDeviceRkb(deviceRkb,TableName);
        }

    }


    @RequestMapping("/update")
    public String updateFT( @RequestBody  Map<String, Object> map) {
        System.out.println("upmap:" + map);
        DeviceRkb deviceRkb = new DeviceRkb();
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        deviceRkb.setRkbName((String) map.get("name"));
        deviceRkb.setRkbState(  Integer.valueOf( map.get("state").toString()));
        deviceRkb.setEquipmentId((Integer) map.get("equipmentId"));
        deviceRkb.setRkbTypeId((String) map.get("rkbType"));
        deviceRkb.setPartId((String) map.get("rkbPartId"));
        deviceRkb.setRmId((String) map.get("rkbRm"));
        deviceRkb.setDescription((String) map.get("description"));
        deviceRkb.setRkbNotes((String) map.get("notes"));
        deviceRkb.setRkbId((Integer) map.get("fid"));
        return  deviceRkbService.updateDeviceRkb(deviceRkb);

    }

    @RequestMapping("/delete")
    public String deleteFT( @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceRkb deviceRkb = new DeviceRkb();
        deviceRkb.setIsDelete(1);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceRkb.setRkbId(id);
                deviceRkbService.deleteDeviceRkb(deviceRkb);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }




    @RequestMapping("/updateState")
    public String updateStateFT(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
        DeviceRkb deviceRkb = new DeviceRkb();
        deviceRkb.setRkbState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceRkb.setRkbId(id);
                deviceRkbService.updateDeviceRkbState(deviceRkb);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }
}
